package com.stylefeng.guns.modular.fileManage.service.impl;

import com.stylefeng.guns.common.persistence.dao.ZyBrewsPictureMapper;
import com.stylefeng.guns.common.persistence.dao.ZyTeaPictureMapper;
import com.stylefeng.guns.common.persistence.model.BrewsSequence;
import com.stylefeng.guns.common.persistence.model.ZyBrewsPicture;
import com.stylefeng.guns.common.persistence.model.ZyPicture;
import com.stylefeng.guns.common.persistence.dao.ZyPictureMapper;
import com.stylefeng.guns.common.persistence.model.ZyTeaPicture;
import com.stylefeng.guns.modular.fileManage.controller.TeaFileController;
import com.stylefeng.guns.modular.fileManage.dao.*;
import com.stylefeng.guns.modular.fileManage.service.IZyPictureService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lsy
 * @since 2017-12-26
 */
@Service
public class ZyPictureServiceImpl extends ServiceImpl<ZyPictureMapper, ZyPicture> implements IZyPictureService {

    @Autowired
    private ZyPictureDao zyPictureDao;

    @Autowired
    private ZyBrewsPictureMapper brewsPictureMapper;

    @Autowired
    private ZyBrewsPictureDao brewsPictureDao;

    @Autowired
    private ZyTeaPictureDao teaPictureDao;

    @Autowired
    private ZyTeaPictureMapper teaPictureMapper;

    private Logger log = LoggerFactory.getLogger(TeaFileController.class);

    @Override
    public List<ZyPicture> selectByTeaId(Integer teaId) {
        List<ZyPicture> zyPictureList = zyPictureDao.selectByTeaId(teaId);
        return zyPictureList;
    }

    /**
     * 将图片id与冲泡方式表的id关联
     *
     * @param brewsId   冲泡方式id
     * @param pictureId 图片id
     */
    @Override
    public void selectorPictureByBrews(Integer brewsId,Integer sequence, Integer pictureId) {
        ZyBrewsPicture zbp = new ZyBrewsPicture();
        zbp.setBrewsId(brewsId);
        zbp.setPictureId(pictureId);
        zbp.setSequence(sequence);

        //查询是否已经存在关联关系
        List<ZyBrewsPicture> originalZbps = brewsPictureDao.selectByBrewsIdAndSequence(brewsId,sequence);
        if (originalZbps == null || originalZbps.size() < 1) {
            //插入关联表记录
            brewsPictureMapper.insert(zbp);
        } else {
            for(int i = 0;i<originalZbps.size();i++){
                //此步骤已有关联的图片时更新关联的图片id
                originalZbps.get(i).setPictureId(pictureId);
                brewsPictureDao.updateById(originalZbps.get(i));
            }
        }
    }


    /**
     * 取消关联指定id集合的冲泡方式图片
     *
     * @param brewsId
     * @param ids
     */
    public void cancelSelectorByBrewsId(Integer brewsId, Integer[] ids) {
        if(ids.length < 1){
            return;
        }
        brewsPictureDao.deleteByPicIdAndBrewsId(brewsId, ids);
    }

    /**
     * 取消关联指定id集合的茶叶图片
     *
     * @param itemId
     * @param pictureIds
     */
    public void cancelSelectorByTeaId(Integer itemId, Integer[] pictureIds) {
        if(pictureIds.length < 1){
            return;
        }
        teaPictureDao.deleteByPicIdAndTeaId(itemId, pictureIds);
    }

    public void selectorPictureByTea(ZyTeaPicture zyTeaPictures) {
        //查询是否已经存在关联关系
        ZyTeaPicture originalZtp = teaPictureDao.selectByTeaIdAndLabAndSequence(zyTeaPictures.getTeaId(),zyTeaPictures.getLabel(),zyTeaPictures.getSequence());
        if (originalZtp == null) {
            //插入关联表记录
            teaPictureMapper.insert(zyTeaPictures);
        } else {
            //此步骤已有关联的图片时更新图片id
            originalZtp.setPictureId(zyTeaPictures.getPictureId());
            teaPictureDao.updateByTeaIdAndLabAndSequence(originalZtp);
        }
    }

    /**
     * 将指定id的图片排序到前面
     *
     * @param picIds   指定的id集合
     * @param pictures 图片的list集合
     */
    public List<ZyPicture> sortByPictureIds(List<Integer> picIds, List<ZyPicture> pictures) {
        //如果id集合和图片list集合的大小一致则无需排序
        if(picIds.size() == pictures.size()){
            return pictures;
        }
        //倒序ids集合
        Collections.reverse(picIds);
        //移除原来 把新的插入第一位
        for (Integer id : picIds) {
            for(int i=0;i<pictures.size();i++){
                if (pictures.get(i).getId().equals(id)) {
                    ZyPicture tempPic = pictures.get(i);
                    pictures.remove(pictures.get(i));
                    pictures.add(0, tempPic);
                    break;
                }
            }
        }
        return pictures;
    }

    @Override
    public List<BrewsSequence> selectBrewsPictures(Integer teaId) {
        List<BrewsSequence> brewsSequenceList=zyPictureDao.selectBrewsPicture(teaId);
        return brewsSequenceList;
    }



}
